﻿namespace Luka;

/// <summary>
/// string类型扩展方法
/// </summary>
public static class StringExtensions
{
    /// <summary>
    /// object转换为字符串
    /// </summary>
    public static string? ToStr(this object value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        if (value is string stringValue)
        {
            return stringValue;
        }
        else if (value is int intValue)
        {
            return intValue.ToString();
        }
        else if (value is double doubleValue)
        {
            return doubleValue.ToString();
        }
        else if (value is float floatValue)
        {
            return floatValue.ToString();
        }
        else if (value is bool boolValue)
        {
            return boolValue.ToString();
        }
        else if (value is DateTime dateValue)
        {
            return dateValue.ToString();
        }
        else
        {
            return string.Empty;
        }
    }

    /// <summary>
    /// 值是否相等
    /// </summary>
    public static bool NotEqual(this string str, string value) => str != value;

    /// <summary>
    /// 取正数
    /// </summary>
    public static decimal ToPositiveNumber(this decimal str) => str < 0 ? 0 : str;

    /// <summary>
    /// 数字格式
    /// </summary>
    public static string ToStrN(this decimal? var) => var.HasValue ? var.Value.ToString("n") : "";

    /// <summary>
    /// 数字格式
    /// </summary>
    public static string ToStrN(this decimal var) => var.ToString("n");

    /// <summary>
    /// 转换为百分比
    /// </summary>
    public static string ToPercent(this decimal? var) => var.HasValue ? (var.Value * 100).ToString("f2") + "%" : "";

    /// <summary>
    /// 转换为百分比
    /// </summary>
    public static string ToPercent(this decimal var) => (var * 100).ToString("f2") + "%";

    /// <summary>
    /// int
    /// </summary>
    /// <returns></returns>
    public static int? ToInt(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (int.TryParse(str, out int result))
            return result;
        return default(int);
    }

    /// <summary>
    /// short
    /// </summary>
    /// <returns></returns>
    public static short? ToShort(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (short.TryParse(str, out short result))
            return result;
        return default(short);
    }

    /// <summary>
    /// long
    /// </summary>
    /// <returns></returns>
    public static long? ToLong(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (long.TryParse(str, out long result))
            return result;
        return default(long);
    }

    /// <summary>
    /// decimal
    /// </summary>
    /// <returns></returns>
    public static decimal? ToDecimal(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (decimal.TryParse(str, out decimal result))
            return result;
        return default(decimal);
    }

    /// <summary>
    /// double
    /// </summary>
    /// <returns></returns>
    public static double? ToDouble(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (double.TryParse(str, out double result))
            return result;
        return default(double);
    }

    /// <summary>
    /// float
    /// </summary>
    /// <returns></returns>
    public static float? ToFloat(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (float.TryParse(str, out float result))
            return result;
        return default(float);
    }

    /// <summary>
    /// DateTime
    /// </summary>
    /// <returns></returns>
    public static DateTime? ToDateTime(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return null;
        if (DateTime.TryParse(str, out DateTime result))
            return result;
        return default(DateTime);
    }

    /// <summary>
    /// bool
    /// </summary>
    /// <returns></returns>
    public static bool ToBool(this string str)
    {
        if (string.IsNullOrWhiteSpace(str)) return false;
        if (bool.TryParse(str, out bool result))
            return result;
        return false;
    }
}